package net.sf.nlp.segment.util;

import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;


public class StringUtility 
{
	public static final String SENTENCE_BEGIN = "始##始";
	public static final String SENTENCE_END = "末##末";
	public static final String SENTENCE_SEPARATORS = "。！？：；…、，（）“”‘’!?:;,()\"'\r\n";
	public static final String BLANK_WORD = "\n\r 　";
	
	public static final String UNKNOWN_PERSON = "未##人";
	public static final String UNKNOWN_SPACE = "未##地";
	public static final String UNKNOWN_NUM = "未##数";
	public static final String UNKNOWN_TIME = "未##时";
	public static final String UNKNOWN_LETTER = "未##串";

	public static final Pattern digitalPattern = Pattern.compile("\\d*");
	public static final Pattern punctuationPattern = Pattern.compile("\\p{Punct}");
	public static final Pattern numberPattern = Pattern.compile("[±\\+\\-－＋]{0,1}[\\d０１２３４５６７８９零○一二两三四五六七八九十廿百千万亿壹贰叁肆伍陆柒捌玖拾佰仟]{0,}(分之){0,1}[\\d０１２３４５６７８９零○一二两三四五六七八九十廿百千万亿壹贰叁肆伍陆柒捌玖拾佰仟]{0,}[．／点]{0,1}[\\d０１２３４５６７８９零○一二两三四五六七八九十廿百千万亿壹贰叁肆伍陆柒捌玖拾佰仟]{1,}[%％‰]{0,1}");
	public static final Pattern dateTimeSuffixPattern1 = Pattern.compile("年|(月份?)|日|时|点|分|秒");
	public static final Pattern dateTimeSuffixPattern2 = Pattern.compile("(年|月)[末内中底前间初]");
	
	public static final String[] numberSuffixs = new String[]{"点", "分之"};
	
	public static boolean isBlank(String word)
	{
		return BLANK_WORD.indexOf(word) >= 0;
	}
	
	public static boolean isChineseWord(String word)
	{
		try 
		{
			if (word.length() == word.getBytes("GBK").length)
				return false;
		} 
		catch (UnsupportedEncodingException e) 
		{
			if (word.length() == word.getBytes().length)
				return false;
		}
		
		return true;
	}
	
	public static boolean isSentenceSeparator(String word)
	{
		return SENTENCE_SEPARATORS.indexOf(word) >= 0;
	}
	
	public static boolean isDigital(String word)
	{
		return digitalPattern.matcher(word).matches();
	}
	
	public static boolean isNumber(String word)
	{		
		boolean matched = numberPattern.matcher(word).matches();
		
		if (matched)
			return true;
		
		String[] items = word.split("-");
		
		if (items.length != 2)
			return false;
		
		for (int i = 0; i < 2; i++)
		{
			if (!numberPattern.matcher(items[i]).matches())
				return false;
		}
		
		return true;
	}
	
	public static boolean isPartNumber(String word)
	{
		for (String suffix : numberSuffixs)
		{
			if (word.endsWith(suffix) && isNumber(word.substring(0, word.length() - suffix.length())))
				return true;
		}
		
		return false;
	}
	
	public static boolean isDateTimeSuffixWord(String word)
	{
		return dateTimeSuffixPattern1.matcher(word).matches() || dateTimeSuffixPattern2.matcher(word).matches();
	}
	
	public static boolean isPunctuation(String word)
	{
		return punctuationPattern.matcher(word).matches();
	}
	
	public static int getLen(String word) 
	{
		try 
		{
			return word != null ? word.getBytes("GBK").length : -1;
		} 
		catch (UnsupportedEncodingException e) 
		{
			e.printStackTrace();
		}
		return -1;
	}
	
	public static void main(String[] args)
	{
		System.out.println(isNumber("百分之五点六"));
	}
}
